/*
 *    Copyright 2008-2009 The Digester Tools Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.digestertools.annotations.handlers;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.apache.commons.digester.Rule;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.digestertools.annotations.AnnotationHandler;
import com.digestertools.annotations.AnnotationRuleProvider;
import com.digestertools.annotations.AnnotationsContext;
import com.digestertools.annotations.CallMethod;
import com.digestertools.annotations.FromAnnotationsRuleSet;
import com.digestertools.annotations.reflect.MethodArgument;
import com.digestertools.annotations.utils.AnnotationUtils;

/**
 * 
 * @author Simone Tripodi
 * @version $Id: AbstractMethodArgumentHanlder.java 365 2009-12-17 08:30:10Z simone.tripodi $
 */
abstract class AbstractMethodArgumentHanlder<A extends Annotation> implements AnnotationHandler<A, MethodArgument> {

    private final Log log = LogFactory.getLog(this.getClass());

    /**
     * {@inheritDoc}
     */
    public final void handle(A annotation, MethodArgument target, AnnotationsContext context, FromAnnotationsRuleSet ruleSet) {
        Method parentMethod = target.getParentMethod();

        if (!parentMethod.isAnnotationPresent(CallMethod.class)) {
            if (this.log.isWarnEnabled()) {
                this.log.warn("Method '"
                        + parentMethod.getName()
                        + "' not annotated by '@"
                        + CallMethod.class.getName()
                        + "', ignoring parameter");
            }

            return;
        }

        CallMethod callMethod = (CallMethod) parentMethod.getAnnotation(CallMethod.class);
        String methodPath = callMethod.pattern();

        String pattern = AnnotationUtils.getAnnotationPattern(annotation);
        ruleSet.addRuleProvider(choosePattern(methodPath, pattern), this.newRuleProvider(annotation, target));
    }

    protected abstract AnnotationRuleProvider<? extends Rule> newRuleProvider(A annotation, MethodArgument target);

    /**
     * 
     * @param defaultPattern
     * @param customPattern
     * @return
     */
    private static String choosePattern(String defaultPattern, String customPattern) {
        if (customPattern == null || customPattern.length() == 0) {
            return defaultPattern;
        }
        return customPattern;
    }

}
